package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Region;
import cn.wolfcode.wolf2w.exception.LogicalException;
import cn.wolfcode.wolf2w.query.DestinationQueryObject;
import cn.wolfcode.wolf2w.query.RegionQueryObject;
import cn.wolfcode.wolf2w.repository.DestinationRepository;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IRegionService;
import cn.wolfcode.wolf2w.util.PagingUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
//@Transactional
public class DestinationServiceImpl implements IDestinationService {

    @Autowired
    private DestinationRepository repository;

    @Autowired
    private IRegionService regionService;

    @Autowired
    private MongoTemplate template;


    @Override
    public void save(Destination destination) {
        repository.save(destination);
    }

    @Override
    public void delete(String id) {
        repository.deleteById(id);

    }

    @Override
    public void update(Destination destination) {
        repository.save(destination);
    }

    @Override
    public Destination get(String id) {
        return repository.findById(id).orElse(null);
    }

    @Override
    public List<Destination> list() {
        return repository.findAll();
    }

    @Override
    public List<Destination> queryDestByRegionId(String rid) {
        //判断id不能为空
        if (!StringUtils.hasLength(rid)){
            return null;
        }
        //得到区域
        Region region = regionService.get(rid);
        //获取目的地集合id
        List<String> refIds = region.getRefIds();

        return repository.findByIdIn(refIds);
    }

    @Override
    public Page<Destination> query(DestinationQueryObject qo) {

        //查询封装类
        Query query = new Query();
        //过滤查询
        if (StringUtils.hasLength(qo.getKeyword())){
            query.addCriteria(Criteria.where("name").regex(qo.getKeyword()));
        }
        //上级id
        if (StringUtils.hasLength(qo.getParentId())){
            query.addCriteria(Criteria.where("parentId").is(qo.getParentId()));
        }else {
            query.addCriteria(Criteria.where("parentId").is(null));
        }

        //查询分页
        Pageable pageable = PageRequest.of(qo.getCurrentPage()-1,
                qo.getPageSize(), Sort.Direction.ASC, "_id");
        //调用工具方法
        return PagingUtil.query(query, template, Destination.class, pageable);

    }



    @Override
    public void changeHotValue(int hot, String id) {
        //校验参数
        if (!StringUtils.hasLength(id)){
            throw new LogicalException("参数异常");
        }
        //方式1
        /*//查询区域对象
        Region region = this.get(id);
        //设置热门
        region.setIshot(hot);
        //更新数据
        this.update(region);*/

        //方式2
        //条件拼接对象
        Query query = new Query();
        //封装条件
        query.addCriteria(Criteria.where("_id").is(id));
        //新建更新类
        Update update = new Update();
        //设置更新数据
        update.set("ishot", hot);
        //执行更新
        template.updateMulti(query, update, Destination.class);

    }

    @Override
    public List<Destination> checkParent(String parentId) {
        //建一个集合存放上级项目
        List<Destination> list = new ArrayList();
        if (!StringUtils.hasLength(parentId)){
            return list;
        }

        /*//查询目的地
        Destination destination = this.get(parentId);
        //添加到集合
        list.add(destination);
        //查询上级id
        String destId = destination.getParentId();
        //当上级不为null
        while (destId != null) {
            //查询上级
            Destination dest = this.get(destId);
            //添加到集合
            list.add(dest);
            //获取上级的id
            destId = dest.getParentId();
        }*/

        //递归
        this.recursion(list, parentId);

        //反转list的方向
        Collections.reverse(list);

        return list;
    }

    //编辑
    @Override
    public void updateInfo(String id, String info) {
        if (!StringUtils.hasLength(id)){
            throw new LogicalException("参数异常");
        }

        Destination destination = this.get(id);
        destination.setInfo(info);
        this.update(destination);
    }

    @Override
    public List<Destination> searchDestination(String regionId) {
        if (!StringUtils.hasLength(regionId)){
            return null;
        }

        List<Destination> list = null;
        //查询国内
        if ("-1".equals(regionId)){
           list = repository.findByParentName("中国");

        }else {

            //查询其他
            list = this.queryDestByRegionId(regionId);
        }

        //遍历集合获取目的地
        for (Destination destination : list) {

            //分页
            Pageable pageable = PageRequest.of(0, 5, Sort.Direction.ASC,"_id");

            //获取子目的地集合
            List<Destination> destinations = repository.findByParentId(destination.getId(),pageable);

            /*//选择5个
            List<Destination> subList = destinations.subList(0, 3);
            //存到children集合中
            destination.setChildren(subList);*/
            destination.setChildren(destinations);
        }

        return list;
    }

    //根据deep查询目的地
    @Override
    public List<Destination> getDestByDeep(int deep) {

        return repository.findByDeep(deep);
    }


    //递归
    public void recursion(List<Destination> list,String parentId){
        if (!StringUtils.hasLength(parentId)){
            return;
        }
        //查询目的地
        Destination destination = this.get(parentId);
        //添加到集合中
        list.add(destination);
        //获取上级id
        parentId = destination.getParentId();
        //判断上级id是否为空
        if (StringUtils.hasLength(parentId)){
            //调用方法
            this.recursion(list, parentId);
        }


    }

}
